// pages/my/my.js
var util = require("../../utils/util.js");
import moment from '../../utils/moment.js'
var app = getApp()
Page({
  data: {
    agentId: '',
    isUserInfo: false,
    userPic: '',
    userName: '',
    shop: '',
    userPhone: '',
    accountId: '',
    nickName: '',
    //订单状态
    memInfo: {
      // waitPAy:2,
      waitDeliver: 0,
      deliver: 0,
      refund: 0,
      addStock: 10
    },
    todayTurnovers: {
      service: "200.23" //本月维修人工费
    },
    cash: '',
    cash_in: '',
    balance: '',
    //权限
    role: '',
    isVisitor: '',
    vipUserName: '',
    vipName: '',
    vipId: '',
    //弹窗
    isMask: false,
    inviteCode: '',
    shopName: '',
    //data
    aellAll: '',
    lirun: '',
    surplusAll: '',
    zonge: '',
  },
  onLoad: function(options) {
    var that = this
    app.getMemInfo().then(res => {
      this.setData({
        agentId: res.data.agentId
      })
    })
  },
  onShow() {
    var that = this
    app.getMemInfo().then(res => {
      this.setData({
        userPhone: res.data.username,
        userName: res.data.name,
        shop: res.data.shop,
        accountId: res.data.id,
        role: res.data.role,
        isVisitor: res.data.isVisitor,
        vipUserName: res.data.vipUserName,
        vipName: res.data.vipName,
        vipId: res.data.vipId
      })
    })
    wx.getUserInfo({
      success(res) {
        console.log(res)
        that.setData({
          isUserInfo: true,
          userPic: res.userInfo.avatarUrl,
          nickName: res.userInfo.nickName
        })
      },
      fail(res) {
        that.setData({
          isUserInfo: false
        })
        console.log(res)
      }
    })
    app.getMemInfo().then(res => {
      console.log(res)
      if (res.data.role == 'VIP') {
        this.getVipData(res.data.vipUserName)
      } else {
        this.getTurnover(res.data.id)
        this.getMoney(res.data.id)
      }
    })

  },
  //营业额
  getTurnover(accountId) {
    var data = {
      accountId
    }
    app.request('wx_app/agent/cashier', data, 'GET').then(res => {
      console.log(res)
      if (res.code == 1) {
        this.setData({
          cash: res.data.cash,
          cash_in: res.data.cash_in
        })
      } else {

      }
    })
  },
  //进货
  waitPayTap: function() {
    wx.switchTab({
      url: 'purchase/purchase'
    })
  },
  //待付款
  waitePay: function() {
    wx.navigateTo({
      url: 'orders/orders?title=待付款&orderType=' + 1,
    })
  },
  //待发货
  waitDeliverTap: function() {
    wx.navigateTo({
      url: 'orders/orders?title=待发货&orderType=' + 2,
    })
  },
  //已发货
  deliverTap: function() {
    wx.navigateTo({
      url: 'orders/orders?title=已发货&orderType=' + 3,
    })
  },
  //退款/售后
  refundTap: function() {
    wx.navigateTo({
      url: 'orders/orders?title=退款/售后&orderType=' + 5,
    })
  },
  //加入库存
  addStockTap: function() {
    wx.navigateTo({
      url: 'addStock/addStock'
    })
  },
  //营业额更多
  moreTurnoverTap(event) {
    wx.navigateTo({
      // url: 'turnover/turnover?type=0'
      url: 'saleData/saleData'
    })
  },
  //客户订单
  totalOrdersTap: function() {
    wx.navigateTo({
      url: 'orders/orders?title=客户订单&orderType=' + 0,
    })
  },
  //地址管理
  addressTap: function() {
    wx.navigateTo({
      url: "myFactory/myFactory",
    })

  },
  //设置
  setTap(event) {
    wx.navigateTo({
      url: 'set/set'
    })
  },
  storeAccountTap: function(event) {
    wx.navigateTo({
      url: 'shopAccount/shopAccount',
    })
  },
  stockManageTap: function(event) {
    wx.navigateTo({
      url: 'stockManage/stockIndex',
    })
  },
  replaceProduct() {
    wx.navigateTo({
      url: 'replaceProduct/replaceProductList',
    })
  },
  toMyWallet() {
    wx.navigateTo({
      url: 'myWallet/myWallet'
    })
  },
  getMoney(accountId) {
    let data = {
      accountId
    }
    app.request('wx_app/agent/account/getByid', data, 'GET').then(res => {
      console.log(res)
      if (res.code == 1) {
        this.setData({
          balance: res.data.agent.accountBalance ? res.data.agent.accountBalance : 0
        })
      } else {

      }

    })
  },
  openInvitation() {
    if (this.data.isVisitor == 1) {
      this.setData({
        isMask: true
      })
    } else {
      console.log('已认证用户')
    }
  },
  getInviteCode(e) {
    this.setData({
      inviteCode: e.detail.value
    })
  },
  bindCancel() {
    this.setData({
      isMask: false
    })
  },
  bindConfirm() {
    let that = this
    let data = {
      accountId: this.data.accountId,
      inviteCode: this.data.inviteCode
    }
    app.request('wx_app/agent_account/input_inviteCode', data, 'GET').then(res => {
      console.log(res)
      if (res.code == 1) {
        wx.setStorage({
          key: 'memInfo',
          data: {
            agentId: res.data.agentId,
            id: res.data.id == null ? 0 : res.data.id,
            gender: res.data.gender,
            username: res.data.username,
            name: res.data.name,
            role: res.data.role,
            isEnable: res.data.isEnable,
            shop: res.data.agentCompanyName,
            isVisitor: res.data.isVisitor,
            vipUserName: res.data.vipUserName,
            phone: res.data.phone,
            vipName: res.data.vipName,
            vipId: res.data.vipId
          },
          success(res) {
            wx.showToast({
              title: '认证成功'
            })
            that.setData({
              isMask: false
            })
          },
          fail(res) {
            app.errorMsg(res)
          }
        })
      } else {
        app.errorMsg(res.message)
      }
    })
  },
  handleContact(e) {
    console.log(e.path)
    console.log(e.query)
  },
  editStoreName() {
    this.setData({
      isMask: true
    })
  },
  getShopName(e) {
    this.setData({
      shopName: e.detail.value
    })
  },
  bindConfirmEdit() {
    if (this.data.role != 'VIP') {
      let data = {
        agentId: this.data.agentId,
        accountId: this.data.accountId,
        companyName: this.data.shopName
      }
      console.log(data)
      app.request('wx_app/agent/company_name/save', data, 'GET').then(res => {
        console.log(res)
        if (res.code == 1) {
          this.setData({
            isMask: false,
            shop: this.data.shopName
          })
          let mem = wx.getStorageSync('memInfo');
          mem.shop = this.data.shopName;
          wx.setStorageSync('memInfo', mem);
        } else {
          app.errorMsg(res.message)
        }
      })
    } else {
      let data = {
        agentAdminName: this.data.vipUserName
      }
      app.request('wx_app/agent_account/editVipName', data, 'GET').then(res => {
        console.log(res)
        if (res.code == 1) {
          this.setData({
            isMask: false,
            shop: this.data.shopName
          })
          let mem = wx.getStorageSync('memInfo');
          mem.vipName = this.data.shopName;
          wx.setStorageSync('memInfo', mem);
        } else {
          app.errorMsg(res.message)
        }
      })
    }
  },
  getVipData(agentAdminName) {
    wx.showLoading({
      title: '正在登录'
    })
    let data = {
      agentAdminName
    }
    console.log(data)
    app.request('wx_app/agent/admin/statistics', data, 'GET').then(res => {
      console.log(res)
      if (res.code == 1) {
        this.setData({
          aellAll: res.data.aellAll == null ? 0 : res.data.aellAll,
          lirun: res.data.lirun == null ? 0 : res.data.lirun,
          surplusAll: res.data.surplusAll == null ? 0 : res.data.surplusAll,
          zonge: res.data.zonge == null ? 0 : res.data.zonge,
        })
        wx.hideLoading()
      } else {
        wx.hideLoading()
        app.errorMsg(res.message)
      }
    })
  },
  bindToVipOrderList() {
    wx.navigateTo({
      url: 'vipOrder/vipOrderList'
    })
  },
  bindToBanner() {

  },
  bindToVipData() {
    wx.navigateTo({
      url: 'vipData/vipData'
    })
  },
})